package  
{
  import com.bit101.components.Label;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.events.TimerEvent;
  import flash.text.TextField;
  import flash.utils.Timer;
	/**
   * ...
   * @author Ivan
   */
  public class Town extends Sprite
  {
    
    public function Town() 
    {
      populationLabel = new Label(this);
      timer.addEventListener(TimerEvent.TIMER, onTimer);
      timer.start();
      onTimer();
      
      addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
      addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
      
      addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
    }
    
    private function onRemoveFromStage(e:Event):void
    {
      removeEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
      
      removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
      removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);  
      removeEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    
    private function onEnterFrame(e:Event):void
    {
      draw();
    }
    
    private function onMouseDown(e:Event):void
    {
      if (owner && !owner.human)
        return;
      
      trace("mouse down on city " + name);
      playerTransfer = new UnitsTransfer();
      playerTransfer.source = this;
      
      parent.setChildIndex(this, 0);
    }
    
    private function onMouseUp(e:Event):void
    {
      trace("mouse up on city " + name);
      if (!playerTransfer) return;
      if (!playerTransfer.source) return;
      playerTransfer.target = this;
      if (playerTransfer.source === playerTransfer.target) return;
      
      // If source city is controlled by AI returns (it may have been losed by player before deploy units).
      if (playerTransfer.source.owner && playerTransfer.source.owner.controller) return;
      
      sendArmy(playerTransfer);
      
      playerTransfer = null;
    }
    
    public function sendArmy(transfer:UnitsTransfer):void
    {
      var army:Army = new Army();
      army.x = transfer.source.x;
      army.y = transfer.source.y;
      army.owner = transfer.source.owner;
      army.target = transfer.target;
      
      army.numSoldiers = transfer.source.population / 2;
      
      transfer.source.population /= 2;
      
      parent.addChild(army);
    }
    
    private function onTimer(e:Event = null):void
    {
      // var grow_factor:Number = 1.05;
      // population *= grow_factor;
      if (population > 0)
        population += 1;
      if (population > populationLimit) population = populationLimit;
      populationLabel.text = ((int)(population)).toString();
    }
    
    private function draw():void
    {
      populationLabel.text = ((int)(population)).toString();
      
      graphics.clear();
      graphics.lineStyle(3, 0x777777);
      
      graphics.beginFill(population > 0 ? owner.color : GameBoard.NEUTRAL_COLOR);
      var w:Number = 5 + populationLimit;
      graphics.drawRect(-w/2, -w/2, w, w);
      // graphics.drawCircle(0, 0, 5 + populationLimit / 2);
      graphics.endFill();
      
      if (population > 0)
      {
        graphics.lineStyle(0);
        graphics.beginFill(0x000000, 0.2);
        w = 5 + population;
        graphics.drawRect(-w/2, -w/2, w, w);
        // graphics.drawCircle(0, 0, 5 + population / 2);
        graphics.endFill();
      }
      
      populationLabel.x = -populationLabel.width / 2;
      populationLabel.y = -populationLabel.height / 2;

      
      // TODO Molaria enganchar el transfer a la ciudad mas cercana.
      if (playerTransfer && !playerTransfer.target && playerTransfer.source == this)
      {
        if (playerTransfer.source.owner && !playerTransfer.source.owner.human) return;
        
        graphics.lineStyle(5, 0x777777, 0.5);
        graphics.beginFill(0x777777, 0.5);
        graphics.moveTo(0, 0);
        graphics.lineTo(mouseX, mouseY);
        graphics.endFill();
      }
    }
    
    private var timer:Timer = new Timer(1000);
    public var population:Number = 0;
    public var populationLimit:Number = 0;
    private var populationLabel:Label;
    public var owner:Player;
    
    private static var playerTransfer:UnitsTransfer;
    
  }

}